package duoxiancheng;

/**
 * 银行取钱
 */
public class UnSafeTest02 {
    public static void main(String[] args) {
        //账户
        Account account = new Account(100,"礼金");
        Drawing you = new Drawing(account,80);
        Drawing wife = new Drawing(account,90);
        new Thread(you,"a").start();
        new Thread(wife,"b").start();
    }
}

//账户
class Account{
    int money;//金额
    String name;//名称

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

//模拟取款
class Drawing implements Runnable{
    Account account;//取钱的账户
    int drawMoney;//取的钱数
    int drawTotal;//取得总数
    int packetTotal;//口袋的钱为

    public Drawing(Account account, int drawMoney) {
        this.account = account;
        this.drawMoney = drawMoney;
    }

    @Override
    public void run() {
        if (account.money-drawMoney<0){
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        account.money-=drawMoney;
        packetTotal+=drawMoney;
        System.out.println(Thread.currentThread().getName()+"--->账户余额为："+account.money);
        System.out.println(Thread.currentThread().getName()+"--->口袋的钱为："+packetTotal);
    }
}